package com.test.ws.s1.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ws.s1.bean.Order;
import com.test.ws.s1.bean.Userqt;
import com.test.ws.s1.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("/order")
    public String order(HttpServletRequest request, Order order) {
        Userqt qttx = (Userqt) request.getSession().getAttribute("qttx");

        List<Order> orders = orderService.selectOrder(order,qttx.getUser_id());
        System.out.println("----------!!!!__________"+orders);
        request.setAttribute("orders",orders);
        return "order";
    }

    /// 后台查分页
    @RequestMapping("/orderCha")
    public String orderCha(HttpServletRequest request, Order order,
                           @RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "5") int pageSize) {
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize);
        ArrayList<Order> orders = (ArrayList<Order>) orderService.selectOrderAll(order);
        PageInfo pageInfo = new PageInfo(orders);
        System.out.println(orders+"====================");
        request.getSession().setAttribute("orderss", orders);
        request.setAttribute("pageInfo",pageInfo);
        return "houtai/orderHt";

    }

    @PostMapping("/orderAdd")
    @ResponseBody
    public boolean orderAdd(HttpServletRequest request, @RequestParam Integer route_id, @RequestParam Integer route_price) {
       Order order = new Order();
        /// 获取用户id
       Userqt qttx = (Userqt) request.getSession().getAttribute("qttx");

       order.setU_id(qttx.getUser_id());
       /// 获取路线id
       order.setS_id(route_id);
       /// 获取路线价格
       order.setOrdenr_price(route_price);
        System.out.println("前台控制器=================================="+qttx);
        int i = orderService.insertOrder(order);
        System.out.println("订单控制器=================================="+i);
        boolean flag;
        if (i > 0) {
            flag = true;
            return flag;
        } else {
            flag = false;
            return flag;
        }
    }

    /// 删除订单
    @RequestMapping("/orderDel")
    public String orderDel(HttpServletRequest request) {
        String order_id = request.getParameter("ord_id");
        Order order = new Order();
        order.setOrder_id(Integer.parseInt(order_id));
        int i = orderService.deleteOrder(order);
        System.out.println(i);
        return "redirect:/order";
    }

}
